package nocannobb.ucanuup.daringshai.activity;

import nocannobb.ucanuup.daringshai.R;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.umeng.analytics.MobclickAgent;

public class BeautyWeb extends Activity {

	private WebView webview;
	private ImageView seekbar;
	private ResetProgressRunnable resetRunnable;

	public static final int LOAD_STATE_STOP = 0;
	public static final int LOAD_STATE_LOADING = 1;
	private static final String TAG = "BeautyWeb";
	public int CURRENT_LOAD_STATE = LOAD_STATE_STOP;

	@Override
	protected void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_webactivity);

		webview = (WebView) findViewById(R.id.webview);
		seekbar = (ImageView) findViewById(R.id.web_progressiv);

		WebSettings webSettings = webview.getSettings();
		webSettings.setJavaScriptEnabled(true);
		webSettings.setPluginState(WebSettings.PluginState.ON);
		webSettings.setPluginsEnabled(true);
		webSettings.setUseWideViewPort(true);
		webSettings.setLoadWithOverviewMode(true);
		webSettings.setLoadsImagesAutomatically(true);
		webview.setWebViewClient(new BaseWebClient());
		webview.setWebChromeClient(new BaseChromeClient());

		String url = getIntent().getStringExtra("url");
		if (!TextUtils.isEmpty(url)) {
			Log.v(TAG, "load url:" + url);
			webview.loadUrl(url);
		}

	}

	private void setImageViewProgress(final View progressBar, int newProgress) {
		Display display = getWindowManager().getDefaultDisplay();
		int screenWidth = display.getWidth();
		int imageWidth = screenWidth * newProgress / 100;
		if (progressBar.getLayoutParams() instanceof FrameLayout.LayoutParams) {
			final android.widget.FrameLayout.LayoutParams params = (android.widget.FrameLayout.LayoutParams) progressBar
					.getLayoutParams();
			params.width = imageWidth;
			progressBar.setLayoutParams(params);
		}
		if (progressBar.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
			final android.widget.RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams) progressBar
					.getLayoutParams();

			params.width = imageWidth;
			progressBar.setLayoutParams(params);
		}
		seekbar.removeCallbacks(resetRunnable);
		if (newProgress == 100) {
			seekbar.postDelayed(resetRunnable, 500);
		}
	}

	public class BaseChromeClient extends WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			super.onProgressChanged(view, newProgress);
			setImageViewProgress(seekbar, newProgress);
		}

		@Override
		public void onReceivedTitle(WebView view, String title) {
			super.onReceivedTitle(view, title);
			if (!TextUtils.isEmpty(title)) {
				return;
			}
			BeautyWeb.this.setTitle(title);
		}
	}

	public class BaseWebClient extends WebViewClient {
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return false;
		}
	}

	private class ResetProgressRunnable implements Runnable {

		@Override
		public void run() {
			setImageViewProgress(seekbar, 0);
		}
	}
}
